package com.notelapierdas.objects;

import java.util.Calendar;

import android.os.CountDownTimer;
import android.widget.TextView;

public class DealTimer extends CountDownTimer{
	TextView textView;
	
	public DealTimer(TextView textView){
		super(getMillisUntilEnd(),1000);
		this.textView=textView;
		//Por ahora lo hago hasta las 23:59 de ese dia		
	}

	@Override
	public void onFinish() {
		textView.setText("Oferta terminada.");
		
	}

	@Override
	public void onTick(long millisUntilFinished) {
		textView.setText(getText(millisUntilFinished));
		
	}
	
	private static long getMillisUntilEnd(){
		int h = 23-Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
		int m = 59-Calendar.getInstance().get(Calendar.MINUTE);
		int s = 59-Calendar.getInstance().get(Calendar.SECOND);
		return ((h*60*60)+(m*60)+s)*1000;		
	}
	
	private String getText(long millis){
		String res;
		int s = (int) (millis/1000);
		int h = s/(60*60);
		s = s-(h*60*60);
		int m = s/60;
		s = s-(m*60);
		res=h+"h "+m+"m "+s+"s";
		return res;
	}

}